package com.kb.design.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kb.design.constant.Constants;
import com.kb.design.dto.BonusAllocationDTO;
import com.kb.design.entity.BonusAllocation;
import com.kb.design.mapper.postgreSQL.BonusAllocationMapper;
import com.kb.design.service.IBonusAllocationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;;
import java.util.ArrayList;
import java.util.List;

/**
 * 奖金配置Service实现类
 * 
 * @author ruoyi
 */
@Service
@DS("slave-1")
public class BonusAllocationServiceImpl extends ServiceImpl<BonusAllocationMapper, BonusAllocation> implements IBonusAllocationService {

    @Resource
    private BonusAllocationMapper bonusAllocationMapper;
    @Override
    @DSTransactional
    public boolean saveBonusAllocation(BonusAllocationDTO bonusAllocation) {
        // 先软删除
        this.update(Wrappers.lambdaUpdate(BonusAllocation.class)
                .set(BonusAllocation::getDelFlag, Constants.DEL_FLAG_YES)
                .eq(BonusAllocation::getDelFlag, Constants.DEL_FLAG_NO));

        List<BonusAllocation> designProject = bonusAllocation.getDesignProject();
        if (CollectionUtil.isNotEmpty(designProject)){
            designProject.stream().forEach(bonusAllocation1 -> {
                bonusAllocation1.setType(Constants.BONUS_ALLOCATION_DESIGN);
                bonusAllocation1.setId(null);
            });
            this.saveBatch(designProject);
        }
        List<BonusAllocation> pageProject = bonusAllocation.getPageProject();
        if (CollectionUtil.isNotEmpty(pageProject)){
            pageProject.stream().forEach(bonusAllocation1 -> {
                bonusAllocation1.setType(Constants.BONUS_ALLOCATION_PAGE);
                bonusAllocation1.setId(null);
            });
            this.saveBatch(pageProject);
        }
        List<BonusAllocation> valorExamine = bonusAllocation.getValorExamine();
        if (CollectionUtil.isNotEmpty(valorExamine)){
            valorExamine.stream().forEach(bonusAllocation1 -> {
                bonusAllocation1.setType(Constants.BONUS_ALLOCATION_VALOR);
                bonusAllocation1.setId(null);
            });
            this.saveBatch(valorExamine);
        }
        if (StringUtils.isNotEmpty(bonusAllocation.getRemark())){
            BonusAllocation remark = new BonusAllocation();
            remark.setRemark(bonusAllocation.getRemark());
            remark.setType(Constants.BONUS_ALLOCATION_REMARK);
            remark.setId(null);
            remark.setDesignNumber(bonusAllocation.getDesignNumber());
            remark.setPageNumber(bonusAllocation.getPageNumber());
            remark.setValorNumber(bonusAllocation.getValorNumber());
            this.save(remark);
        }
        return true;
    }

    @Override
    public BonusAllocationDTO getBonusAllocation(BonusAllocationDTO bonusAllocation) {
        BonusAllocationDTO bonus = new BonusAllocationDTO();
        List<BonusAllocation> list = bonusAllocationMapper.selectList(Wrappers.lambdaQuery(BonusAllocation.class).eq(BonusAllocation::getDelFlag, Constants.DEL_FLAG_NO));
        if (CollectionUtil.isNotEmpty(list)){
            List<BonusAllocation> designProject = new ArrayList<>();
            List<BonusAllocation> pageProject = new ArrayList<>();
            List<BonusAllocation> valorExamine = new ArrayList<>();
            for (BonusAllocation allocation : list) {
                //type = 1 代表 奖金配置类型为 设计项目
                if (allocation.getType().equals(Constants.BONUS_ALLOCATION_DESIGN)){
                    designProject.add(allocation);
                }else if (allocation.getType().equals(Constants.BONUS_ALLOCATION_PAGE)){
                    pageProject.add(allocation);
                }
                else if (allocation.getType().equals(Constants.BONUS_ALLOCATION_REMARK)){
                    bonus.setRemark(allocation.getRemark());
                    bonus.setDesignNumber(allocation.getDesignNumber());
                    bonus.setPageNumber(allocation.getPageNumber());
                    bonus.setValorNumber(allocation.getValorNumber());
                    bonus.setId(allocation.getId());
                }
                else {
                    valorExamine.add(allocation);
                }
            }
            bonus.setDesignProject(designProject);
            bonus.setPageProject(pageProject);
            bonus.setValorExamine(valorExamine);
        }
        return bonus;
    }
}
